# -*- coding: utf-8 -*-

import os

from datetime import datetime
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.db import Key 
from google.appengine.ext.webapp import template
from google.appengine.api import users
from django.utils import simplejson


from baseclass import BaseHandler
from model import Customer

class CustomerModifyJSON(BaseHandler):
    def get(self):
        query = db.Query(Customer)
        query.filter('email =', self.request.get("email"))
        customer = query.get()
        if customer:
            json = {'email':customer.email,
                    'name':customer.name,
                    'phone':customer.phone,
                    'address':customer.address,
                    'date':str(customer.date),
                    'key':str(customer.key())}
        else:
            json = {'email':self.request.get("email"),
                    'name':'',
                    'phone':'',
                    'address':'',
                    'date':'',
                    'key':''}
        body = simplejson.dumps(json)
        self.response.out.write(body)
        
    def post(self):
        try:
            customer = Customer.get(Key(self.request.get("key")))
        except:
            customer = Customer()
        customer.email = self.request.get("email")
        customer.name = self.request.get("name")
        customer.phone = self.request.get("phone")
        customer.address = self.request.get("address")
        customer.put()        
        self.response.out.write("ok")
        
class CustomerModify(BaseHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            query = db.Query(Customer)
            query.filter('email =', user.email())
            customer = query.get()
            template_values = {
                               'email': user.email(),
                               'customer':customer                               
                               }       
        else:
            template_values = {'msg':'not login'}
        self.outputTemplate(template_values, "customer_modify_view.html")
            
    def post(self):
        user = users.get_current_user()
        if user:
            query = db.Query(Customer)
            query.filter('email =', user.email())
            customer = query.get()
            if not customer:
                customer = Customer()
                customer.email = user.email()
            customer.name = self.request.get("name")
            customer.phone = self.request.get("phone")
            customer.address = self.request.get("address")
            customer.put()
            template_values = {'msg':'ok'}
        else:
            template_values = {'msg':'not login'}
        self.outputTemplate(template_values, "customer_modify_view.html")
                   
        
        
application = webapp.WSGIApplication(
                                     [('/customer/modify', CustomerModify),
                                      ('/customer/modify/json', CustomerModifyJSON)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
    